<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 角色验证器
//-------------------------

namespace app\common\validate;

use think\Validate;
use think\Db;

class AdminRole extends Validate
{
    protected $rule = [
        "name|名称"   => "require|checkNode:1",
        "status|状态" => "require",
    ];
    /**
     * 验证节点是否唯一
     */
    protected function checkNode($value, $rule, $data)
    {
        if (isset($data['id']) && $data['id']) {
            $where['id'] = ["neq", $data['id']];
            $where['name'] = $data['name'];
            $where['isdelete'] = 0;
        }else{
            $where['admin_reg'] = $data['admin_reg'];
            $where['name'] = $data['name'];
            $where['isdelete'] = 0;
        }
       
        return Db::name("AdminRole")->where($where)->find() ? "名称已经存在" : true;
    }
}